package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.constant.reasonConstant;
import com.li.mapper.BusinessMapper;
import com.li.pojo.dto.BusinessDTO;
import com.li.pojo.dto.BusinessReasonDTO;
import com.li.pojo.po.Opportunities;
import com.li.pojo.po.OpportunitiesRecord;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.User;
import com.li.pojo.vo.AssignmentVo;
import com.li.service.BusinessService;
import com.li.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private UserService userService;

    /**
     * 商机分页
     * @param businessDTO
     * @return
     */
    @Override
    public PageResult listPage(BusinessDTO businessDTO) {
        PageHelper.startPage(businessDTO.getPageNum(),businessDTO.getPageSize());
        Page<Opportunities> page = businessMapper.listPage(businessDTO.getPageNum(),businessDTO.getPageSize(),businessDTO.getId(),businessDTO.getPhone(),businessDTO.getName(),businessDTO.getOwner(),businessDTO.getStatus(),businessDTO.getParams().get("beginCreateTime"),businessDTO.getParams().get("endCreateTime"));
        PageResult pageResult = new PageResult();
        Object params = pageResult.getParams();
        return new PageResult(200,"查询成功",params,page.getResult(),page.getTotal());
    }

    /**
     * 公海池分页
     * @param businessReasonDTO
     * @return
     */
    @Override
    public PageResult poolPage(BusinessReasonDTO businessReasonDTO) {
        PageHelper.startPage(businessReasonDTO.getPageNum(),businessReasonDTO.getPageSize());
        Page<Opportunities> page = businessMapper.poolPage(businessReasonDTO.getPageNum(),businessReasonDTO.getPageSize(),businessReasonDTO.getId(),businessReasonDTO.getPhone(),businessReasonDTO.getName(),businessReasonDTO.getSubject(),businessReasonDTO.getParams().get("beginCreateTime"),businessReasonDTO.getParams().get("endCreateTime"));
        PageResult pageResult = new PageResult();
        Object params = pageResult.getParams();
        return new PageResult(200,"查询成功",params,page.getResult(),page.getTotal());
    }

    /**
     * 商机新增
     * @param opportunities
     */
    @Override
    public void listSave(Opportunities opportunities) {
        opportunities.setCreateTime(LocalDateTime.now().toString().replace("T"," "));
        opportunities.setOwnerTime(LocalDateTime.now().toString().replace("T"," "));
        opportunities.setOwner("admin");
        opportunities.setAssignBy("admin");
        opportunities.setCreateBy("admin");
        opportunities.setStatus("1");
        opportunities.setReason("0");
        businessMapper.listSave(opportunities);
    }

    /**
     * 商机详情
     * @param id
     * @return
     */
    @Override
    public Opportunities find(Integer id) {
        Opportunities opportunities = businessMapper.find(id);
        return opportunities;
    }

    /**
     * 商机修改
     * @param opportunities
     */
    @Override
    public void update(Opportunities opportunities) {
        businessMapper.update(opportunities);
    }

    /**
     * 新增沟通记录
     * @param opportunities
     */
    @Override
    public void recordAdd(Opportunities opportunities) {
        OpportunitiesRecord record = new OpportunitiesRecord();
        BeanUtils.copyProperties(opportunities,record);
        record.setCreateBy("admin");
        record.setCreateTime(LocalDateTime.now().toString().replace("T"," "));
        businessMapper.recordAdd(record);
        businessMapper.update(opportunities);
    }

    /**
     * 沟通记录展示
     * @param id
     * @return
     */
    @Override
    public List<OpportunitiesRecord> recordList(Long id) {
        List<OpportunitiesRecord> records = businessMapper.recordList(id);
        return records;
    }

    /**
     * 踢回公海
     * @param id
     * @param reason
     * @return
     */
    @Override
    public Integer backReason(Integer id, String reason) {
        Integer i = businessMapper.back(id,reason);
        return i;
    }

    /**
     * 捞取
     * @param assignmentVo
     */
    @Override
    public void reasonGain(AssignmentVo assignmentVo) {
        for (Integer id : assignmentVo.getIds()) {
            businessMapper.reasonGain(id, reasonConstant.BUSINESS);
        }
    }

    @Override
    public void Assignment(AssignmentVo assignmentVo) {
        log.info("assignmentVo:{}",assignmentVo);
        User user = userService.getById(assignmentVo.getUserId());
        Opportunities opportunities = new Opportunities();
        for (Integer id : assignmentVo.getIds()) {
            log.info("id:{}",id);
            opportunities.setAssignId(Long.valueOf(user.getUserId()));
            opportunities.setOwner(user.getUserName());
            opportunities.setBusinessId(id+"");
            log.info("opportunities:{}",opportunities);
            businessMapper.update(opportunities);
        }

    }

}
